/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tagHandlers;

import DatabaseAccessing.ProductModelController;
import DatabaseAccessing.Products;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.hibernate.Session;

/**
 *
 * @author MaramZahran
 */
public class ProductTagHandler extends SimpleTagSupport {

    private int pageNumber;

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {
            System.out.println("Inside the handler");
            ProductModelController pMC = new ProductModelController();
            List pro = pMC.viewSomeProduct(pageNumber);
            getJspContext().removeAttribute("category");

            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");
            Products p = null;
            //   out.println("<pg:paging>");
            System.out.println(pageNumber + "**********************");
            //int startingPoint= 20*(pageNumber-1 );
            for (int i = 0; i < pro.size(); i++) //for(int i = 0 ; i <20 ; i++)
            {

                p = (Products) pro.get(i);
                if (p.getAmount() != 0) {
                    //p=(Products) pro.get(i+startingPoint);
                    //     out.println("<pg:item>");
                    out.println("<div class=\"prod_box\">");
                    out.println("<div class=\"top_prod_box\"></div>");
                    out.println("<div class=\"center_prod_box\">");
                    out.println("<div class=\"product_title\"><a href=\"ViewProduct.jsp" + "?ProductName=" + p.getProductName() + "&ProductPrice=" + p.getPrice() + "&ProductAmount=" + p.getAmount()
                            + "&ProductImage=" + p.getProductImage() + "&ProductNote=" + p.getProductNotes() + "\">" + p.getProductName() + "</a></div>");
                    out.println("<div class=\"product_img\"><a href=\"ViewProduct.jsp" + "?ProductName=" + p.getProductName() + "&ProductPrice=" + p.getPrice() + "&ProductAmount=" + p.getAmount() + "&ProductID=" + p.getProductId()
                            + "&ProductImage=" + p.getProductImage() + "&ProductNote=" + p.getProductNotes() + "&ProductID=" + p.getProductId() + "\"><img src=\"productImages/" + p.getProductImage() + "\" alt=\"\" width=\"150px\" height=\"100px\" title=\"\" border=\"0\" /></a></div>");
                    out.println("<div class=\"prod_price\"><span class=\"price\">" + p.getPrice() + "</span></div>");
                    out.println("</div><div class=\"bottom_prod_box\"></div><div class=\"prod_details_tab\">");
                    out.println("<a href=\"AddToCart?ProductID=" + p.getProductId().toString() + "\" title=\"header=[Add to cart] body=[&nbsp;] fade=[on]\"><img src=\"images/cart.gif\" alt=\"\" title=\"\" border=\"0\" class=\"left_bt\" /></a>");
                    out.println("<a href=\"ViewProduct.jsp" + "?ProductName=" + p.getProductName() + "&ProductPrice=" + Float.toString(p.getPrice()) + "&ProductAmount=" + p.getAmount()
                            + "&ProductImage=" + p.getProductImage() + "&ProductNote=" + p.getProductNotes() + "&ProductID=" + p.getProductId() + "\" class=\"prod_details\">details</a></div></div>");
                }
                // out.println("</pg:item>");
            }
            //out.println(" <pg:index title=\"More:\"><pg:page><%=thisPage%></pg:page></pg:index>");
            //out.println("</pg:paging>");

//                out.println("Here"+pro.size());
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }



            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProductTagHandler tag", ex);
        }
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }
}
